<?php

declare (strict_types=1);

namespace App\Model;

use App\Utils\Common;
use App\Utils\Upload\View;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;

/**
 * Class User
 * @package App\Model
 * @property mixed|int id
 * @property mixed|int cooperative_id
 * @property mixed|string nickname
 * @property mixed|string phone
 * @property mixed|string password
 * @property mixed|string avatar
 * @property mixed|int personal_certification
 * @property mixed|int level
 * @property mixed|int credit_point
 * @property mixed|int commission
 * @property mixed|int award
 * @property mixed|int status
 * @property mixed|int is_employer
 * @property mixed|string alipay
 * @property mixed|string created_at
 * @property mixed|int updated_at
 * @property mixed|int deleted_at
 * @property mixed|string wx_mp_openid
 * @property int company_certification
 * @property int withdraw_gold
 * @property int done_task_exec_count
 * @property-read float withdrawAmount
 *
 * @property UserInfo info
 * @property UserEmployer employer
 * @property CertificationCompanyApply certificationCompanyApply
 * @property Cooperative cooperative
 */
class User extends Model
{
    use SoftDeletes;

    protected $table = 'user';

    protected $guarded = [];

    protected $casts = [];

    protected $fillable = [
        'cooperative_id',
        'nickname',
        'phone',
        'level',
        'status',
        'credit_point',
        'avatar',
        'is_employer',
        'password'
    ];

    /**
     * 隐藏属性
     * @var array
     */
    protected $hidden = [
        'password',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    public function employer()
    {
        return $this->hasOne(UserEmployer::class, 'user_id', 'id');
    }

    public function info()
    {
        return $this->hasOne(UserInfo::class, 'user_id', 'id');
    }

    public function certificationCompanyApply()
    {
        return $this->hasOne(CertificationCompanyApply::class, 'user_id', 'id');
    }

    public function cooperative()
    {
        return $this->belongsTo(Cooperative::class, 'cooperative_id', 'id');
    }

    public function taskExecs()
    {
        return $this->hasMany(TaskExec::class);
    }

    public function getWithdrawAmountAttribute()
    {
        return bcdiv((string)$this->attributes['withdraw_gold'], '100', 2);
    }

    public function getAvatarAttribute($value)
    {
        if ($value) {
            if (Common::checkResource('client')) {
                return [
                    'image_id' => $value,
                    'image_path' => di(View::class)->fullPath($value)
                ];
            } else {
                return di(View::class)->fullPath($value);
            }
        } else {
            return '';
        }
    }

}